home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / docs / misc / MemManual.lha / MemManual / C_Sources / vmem.c < prev   
Encoding:
C/C++ Source or Header  |  2002-02-09  |  11.2 KB  |  338 lines

  1. /*************************************************
  2.  ** vmem                                        **
  3.  **                                             **
  4.  ** sample file-memory mapping using the        **
  5.  ** memory.library                              **
  6.  ** © 2001 THOR Software, Thomas Richter        **
  7.  *************************************************/
  8.  
  9. /// Includes
  10. #include <exec/types.h>
  11. #include <exec/libraries.h>
  12. #include <utility/tagitem.h>
  13. #include <mmu/mmutags.h>
  14. #include <memory/memory.h>
  15. #include <memory/memtags.h>
  16. #include <dos/rdargs.h>
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/mmu.h>
  20. #include <proto/dos.h>
  21. #include <proto/memory.h>
  22.  
  23. #include <strings.h>
  24. #include <math.h>
  25. ///
  26. /// Defines
  27. #define TEMPLATE "SWAP/A"
  28.  
  29. #define ARG_SWAP        0L
  30. #define ARG_NUM         1L
  31. ///
  32. /// Statics
  33. char version[]="$VER: vmem 40.1 (9.2.2002) © THOR";
  34. struct ExecBase *SysBase;
  35. struct MemoryLibrary *MemoryBase;
  36. struct MMUBase *MMUBase;
  37. struct DosLibrary *DOSBase;
  38. ///
  39. /// Protos
  40. int __asm __saveds main(void);
  41. int PoolTest(struct VMPool *p);
  42. long rangerand(long max);
  43. struct Keeper **indextokp(struct Keeper **first,long index);
  44. int VMemTest(char *name);
  45. ///
  46. /// Structures
  47. struct Keeper {
  48.         struct Keeper   *next;
  49.         ULONG            len;
  50. };
  51. ///
  52.  
  53. /// main
  54. int __asm __saveds main(void)
  55. {
  56. struct RDArgs *rd;
  57. LONG args[ARG_NUM];
  58. int rc = 25;
  59.  
  60.         SysBase = *((struct ExecBase **)(4L));
  61.  
  62.         memset(args,0,sizeof(args));
  63.  
  64.         if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",37)) {
  65.                 if (MMUBase = (struct MMUBase *)OpenLibrary("mmu.library",43)) {
  66.                         if (MemoryBase = (struct MemoryLibrary *)OpenLibrary("memory.library",0)) {
  67.                                 Printf("memory.library successfully openend.\n");
  68.                                 if (rd = ReadArgs(TEMPLATE,args,NULL)) {
  69.                                         rc = VMemTest((char *)(args[ARG_SWAP]));
  70.                                         FreeArgs(rd);
  71.                                 } else {
  72.                                         PrintFault(IoErr(),"vmem failed");
  73.                                 }
  74.                                 CloseLibrary((struct Library *)MemoryBase);
  75.                         } else {
  76.                                 Printf("vmem failed: Failed to open the memory.library.\n");
  77.                         }
  78.                         CloseLibrary((struct Library *)MMUBase);
  79.                 } else {
  80.                         Printf("mmap failed: Failed to open the mmu.library V43.");
  81.                 }
  82.                 CloseLibrary((struct Library *)DOSBase);
  83.         }
  84.  
  85.         return rc;
  86. }
  87. ///
  88. /// VMemTest
  89. int VMemTest(char *name)
  90. {
  91. struct MMUContext *ctx;
  92. struct AdrSpace *adr;
  93. struct VMPool *vmpool;
  94. int rc = 10;
  95. int len;
  96.  
  97.         /*
  98.          * create a new MMU Context to operate in
  99.          */
  100.         ctx = CreateMMUContext(MCXTAG_SHARE,CurrentContext(NULL),TAG_DONE);
  101.  
  102.         if (ctx == NULL)
  103.                 Printf("Failed to create a new MMU context.\n");
  104.  
  105.         /*
  106.          * Check whether the user wants a swap file or a swap partition
  107.          */
  108.         len = strlen(name);
  109.         if ((len > 0) && (name[len-1]==':')) {
  110.                 char in[256];
  111.  
  112.                 Printf("You selected a swap partition for this test suite.\n"
  113.                        "Remember that all data on partition %s will be erased.\n"
  114.                        "Are you sure that you want to do this?\n"
  115.                        "(yes/no):");
  116.                 Flush(Output());
  117.  
  118.                 len = Read(Input(),in,255);
  119.                 if (len != 4 || (strcmp(in,"yes\n"))) {
  120.                         Printf("***Aborted***\n");
  121.                         return 5;
  122.                 }
  123.  
  124.                 adr = NewAdrSpace(      MEMTAG_MAXSYSMEM,64<<12,
  125.                                         MEMTAG_VMSWAPTYPE,MEMFLAG_SWAPPART,
  126.                                         MEMTAG_SWAPPARTNAME,name,
  127.                                         MEMTAG_VMMAXSIZE,64*1024*1024, // 64MB
  128.                                         MEMTAG_PROVIDESMEM,TRUE,
  129.                                         MEMTAG_CONTEXT,ctx,
  130.                                         TAG_DONE);
  131.         } else {
  132.                 adr = NewAdrSpace(      MEMTAG_MAXSYSMEM,25<<12,
  133.                                         MEMTAG_VMSWAPTYPE,MEMFLAG_SWAPFILE,
  134.                                         MEMTAG_SWAPFILENAME,name,
  135.                                         MEMTAG_VMSIZE,64*1024*1024, // 64MB
  136.                                         MEMTAG_PROVIDESMEM,TRUE,
  137.                                         MEMTAG_CONTEXT,ctx,
  138.                                         TAG_DONE);
  139.         }
  140.  
  141.         if (adr) {
  142.                 Printf("Creating the new address space succeeded.\n"
  143.                        "Address space is: %08lx\n",adr);
  144.  
  145.                 if (EnterAddressSpace(adr,NULL)) {
  146.                         Printf("Entered the address space successfully.\n");
  147.  
  148.  
  149.                         vmpool = NewVMPool(     MEMTAG_ADRSPACE,adr,
  150.                                                 MEMTAG_PROVIDESMEM,TRUE,
  151.                                                 //MEMTAG_PUDDLESIZE,5<<12,
  152.                                                 //MEMTAG_MEMFLAGS,MEMF_CLEAR,
  153.                                                 TAG_DONE);
  154.  
  155.                         if (vmpool) {
  156.                                 Printf("Created the new pool succesfully.\n"
  157.                                        "Pool is: %08lx\n",vmpool);
  158.  
  159.  
  160.                                 rc = PoolTest(vmpool);
  161.  
  162.                                 DeleteVMPool(vmpool);
  163.                         } else Printf("mmap failed: Could not create new pool.\n");
  164.  
  165.                         LeaveAddressSpace(NULL);
  166.                 } else Printf("mmap failed: Failed to enter the address space.\n");
  167.  
  168.                 DeleteAdrSpace(adr);
  169.         } else Printf("mmap failed: Could not create new address space.\n");
  170.  
  171.         DeleteMMUContext(ctx);
  172.  
  173.         return rc;
  174. }
  175. ///
  176. /// PoolTest
  177. int PoolTest(struct VMPool *pool)
  178. {
  179. unsigned short seed[3];
  180. long index=0,delta;
  181. struct Keeper **kpp,*kp;
  182. ULONG max;
  183. ULONG total = 0;
  184. ULONG mems = 0;
  185. ULONG meter = 0;
  186. struct Keeper *first[256];
  187. UBYTE fidx = 0;
  188. ULONG acnt = 0;
  189. struct DateStamp orig,ds;
  190.  
  191.         memset(first,0,sizeof(first));
  192.         DateStamp(&ds);
  193.         /*
  194.         seed[0]=ds.ds_Tick;
  195.         seed[1]=ds.ds_Tick>>16;
  196.         seed[2]=ds.ds_Minute;
  197.         */
  198.         seed[0]=1;
  199.         seed[1]=1;
  200.         seed[2]=2;
  201.         seed48(seed);
  202.         max = 64*1024*1024;
  203.  
  204.         Printf("Start messing the (virtual) memory....\n");
  205.         DateStamp(&orig);
  206.  
  207.         do{
  208.  
  209.                 if (((lrand48() & 0x3fff)>((meter>2500)?0x2400:0x1a00)) && (max>0)) {
  210.                         delta=0;
  211.                         switch(lrand48() & 0x7000){
  212.                                 case 0x1000:
  213.                                 case 0x2000:
  214.                                 case 0x3000:
  215.                                         index=rangerand(0x100);
  216.                                         break;
  217.                                 case 0x4000:
  218.                                         index=rangerand(0x200);
  219.                                         break;
  220.                                 case 0x5000:
  221.                                         index=rangerand(0x800);
  222.                                         delta=lrand48() & 0x03;
  223.                                         break;
  224.                                 case 0x6000:
  225.                                         index=rangerand(max);
  226.                                         delta=lrand48() & 0x1f;
  227.                                         break;
  228.                                 case 0x7000:
  229.                                         index=rangerand(max);
  230.                                         delta=lrand48() & 0x07;
  231.                                         break;
  232.                         }
  233.                         if (delta)
  234.                                 index >>= delta;
  235.                         index &= (~7);
  236.                         if ((index>0) && (index<=max)) {
  237.                                 Printf("Allocating");
  238.                                 Flush(Output());
  239.                                 acnt++;
  240.                                 if (kp=AllocVMemory(pool,index)) {
  241.                                         Printf(" %08lx bytes at %08lx.\n",index,kp);
  242.                                         kp->next=first[fidx];
  243.                                         kp->len=index;
  244.                                         first[fidx]=kp;
  245.                                         mems++;
  246.                                         max-=index;
  247.                                         total+=index;
  248.                                         fidx++;
  249.                                 } else Printf(" %08lx bytes failed.\n",index);
  250.                         }
  251.                 } else {
  252.                         if (mems>0) {
  253.                                 index=rangerand(mems);
  254.                                 kpp=indextokp(first,index);
  255.                                 if (kpp) {
  256.                                         ULONG len;
  257.                                         kp=*kpp;
  258.                                         *kpp=kp->next;
  259.                                         max+=kp->len;
  260.                                         total-=kp->len;
  261.                                         Printf("Releasing ");
  262.                                         len = kp->len;
  263.                                         Flush(Output());
  264.                                         acnt++;
  265.                                         FreeVMemory(pool,kp,kp->len);
  266.                                         Printf(" %08lx bytes at %08lx.\n",len,kp);
  267.                                         mems--;
  268.                                 }
  269.                         }
  270.                 }
  271.  
  272.                 meter++;
  273.                 if (CheckSignal(SIGBREAKF_CTRL_D)) {
  274.                         LONG ticks;
  275.  
  276.                         DateStamp(&ds);
  277.  
  278.                         ticks = ds.ds_Days - orig.ds_Days;
  279.                         ticks = (ds.ds_Minute - orig.ds_Minute) + (ticks * 24 * 60);
  280.                         ticks = (ds.ds_Tick - orig.ds_Tick) + (ticks * 60 * 50);
  281.                         if (acnt)       ticks  = (ticks * 100) / acnt;
  282.                         else            ticks  = 0;
  283.  
  284.  
  285.                         Printf("Holding 0x%08lx bytes in %ld blocks. Time per allocation: %ld\n",total,mems,ticks);
  286.  
  287.                         DateStamp(&orig);
  288.                         acnt = 0;
  289.                 }
  290.                 if (meter>5000)
  291.                         meter=0;
  292.  
  293.         }while(!CheckSignal(SIGBREAKF_CTRL_C));
  294.  
  295.         Printf("*** Break\n");
  296.  
  297.         fidx = 0;
  298.         do {
  299.                 while(first[fidx]) {
  300.                         kp=first[fidx];
  301.                         first[fidx]=kp->next;
  302.                         FreeVMemory(pool,kp,kp->len);
  303.                 }
  304.         } while(++fidx);
  305.  
  306.         Printf("Done.\n");
  307.  
  308.         return 0;
  309. }
  310. ///
  311. /// rangerand
  312. long rangerand(long max)
  313. {
  314.         return (long)(((ULONG)(lrand48()))%((ULONG)(max)));
  315. }
  316. ///
  317. /// indextokp
  318. struct Keeper **indextokp(struct Keeper **first,long index)
  319. {
  320. struct Keeper **kpp = first+(index & 0xff);
  321.  
  322.         index >>= 8;
  323.         while(index>0){
  324.                 if (*kpp == NULL)
  325.                         return NULL;
  326.  
  327.                 kpp=&((*kpp)->next);
  328.                 index--;
  329.         }
  330.  
  331.         if (*kpp == NULL)
  332.                 return NULL;
  333.  
  334.         return kpp;
  335. }
  336. ///
  337.  
  338.